﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Mono.Data.Sqlite;

public class JYDBMethod : ISingleton<JYDBMethod>
{
    /// <summary>
    /// SQLite数据库辅助类
    /// </summary>
    private DataManager dManager = null;
    private DataManager dataManager
    {
        get
        {
            if (dManager == null)
            {
                //sql = new SQLiteHelper(CGameDefine.Sqlite_PATH_PRFIX);
                dManager = DataManager.Instance;
            }
            return dManager;
        }
    }

    /// <summary>
    /// 获得英雄
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public bool AddHero(int id)
    {
        Hero hero = new Hero();
        hero.heroID = id;
        hero.exp = 0;
        hero.level = 0;
        AddHero(hero);
        return true;
    }

    public bool AddHero(Hero hero)
    {
        dataManager.Add<Hero>(ref hero);
        return true;
    }

    /// <summary>
    /// 阵位添加英雄
    /// </summary>
    /// <param name="arrayIndex"></param>
    /// <param name="heroID"></param>
    /// <returns></returns>
    public bool AddHeroToBattleArray(int arrayIndex, int heroID)
    {
        BattleArray bArray = new BattleArray();
        bArray.arrayType = (char)arrayIndex;
        bArray.heroID = heroID;
        dataManager.Add<BattleArray>(ref bArray);
        return true;
    }

    /// <summary>
    /// 改变阵中英雄
    /// </summary>
    /// <param name="arrayIndex">所在阵位</param>
    /// <param name="heroID">改变的英雄ID</param>
    /// <returns></returns>
    public bool ChangeBattleArrayHero(int arrayIndex, int heroID)
    {
        DBAdapter dbAdapter = new DBAdapter();
        dbAdapter.Add("arrayType", arrayIndex);
        DBAdapter changeAdapter = new DBAdapter();
        changeAdapter.Add("heroID", heroID);
        dataManager.Update<BattleArray>(dbAdapter, changeAdapter);

        return true;
    }

    public bool ChangeBattleArrayHero(int arrayIndex, Hero hero)
    {
        ChangeBattleArrayHero(arrayIndex, hero.id);
        return true;
    }

    /// <summary>
    /// 获得阵中英雄
    /// </summary>
    /// <returns></returns>
    public List<BattleArray> GetBattleArrayHero()
    {
        List<BattleArray> list = dataManager.QueryList<BattleArray>();
        for (int i = 0; i < list.Count; i++)
        {
            BattleArray array = list[i];
            Hero hero = dataManager.QueryById<Hero>(array.heroID);
            if (hero == null)
            {
                Debugger.LogError("Can't find the data of the hero[" + array.heroID + "] from the table.");
                continue;
            }
            array.level = hero.level;
            array.exp = hero.exp;
        }
        return list;
    }

    //private Dictionary<int, Hero> GetHero()
    //{
    //    Dictionary<int, Hero> dicHero = null;
    //    DBAdapter ad = new DBAdapter();
    //    SqliteDataReader reader = Sql.QueryList<Hero>();
    //    while (reader.Read())
    //    {
    //        dicHero = new Dictionary<int, Hero>();
    //        Hero hero = new Hero();
    //        hero.id = reader.GetInt32(reader.GetOrdinal("ID"));
    //        hero.heroID = reader.GetInt32(reader.GetOrdinal("heroID"));
    //        hero.level = reader.GetInt32(reader.GetOrdinal("level"));
    //        hero.exp = reader.GetInt32(reader.GetOrdinal("exp"));
    //        dicHero.Add(hero.heroID, hero);
    //    }
    //    return dicHero;
    //}

    /// <summary>
    /// 训练英雄获得经验
    /// </summary>
    /// <param name="id"></param>
    /// <param name="exp"></param>
    /// <returns></returns>
    public bool ChangeExpForHero(int id, int exp)
    {
        return true;
    }

    /// <summary>
    /// 添加武器
    /// </summary>
    /// <param name="ID">武器ID</param>
    /// <param name="arrayIndex">武器类型</param>
    /// <returns></returns>
    public bool AddEquipment(int ID, int arrayIndex, int BattleArrayID)
    {
        Equipment equip = new Equipment();
        equip.EquipID = ID;
        equip.ArrayIndex = arrayIndex;
        equip.BattleArrayID = BattleArrayID;
        AddEquipment(equip);
        return true;
    }

    public bool AddEquipment(Equipment equip)
    {
        dataManager.Add<Equipment>(ref equip);
        _saveEquipInBattle(equip);
        return true;
    }

    public bool CheckExistEquipment(int equipID)
    {
        return SelectEquipment(equipID) != null;
    }

    public Equipment SelectEquipment(int equipID)
    {
        DBAdapter ad = new DBAdapter();
        ad.Add("EquipID", equipID);
        List<Equipment> list = dataManager.QueryList<Equipment>(ad);
        if (null == list || list.Count == 0)
        {
            return null;
        }
        else
        {
            if (list.Count > 1)
            {
                Debugger.LogError("The equipment[" + equipID + "] is using in two or more poistion.");
            }
        }
        return list[0];
    }

    public bool UpdateEquipment(Equipment eq)
    {
        DBAdapter ad = new DBAdapter();
        ad.Add("EquipID", eq.EquipID);
        DBAdapter cad = new DBAdapter();
        cad.Add("BattleArrayID", eq.BattleArrayID);
        dataManager.Update<Equipment>(ad, cad);

        _saveEquipInBattle(eq);
        return true;
    }

    private void _saveEquipInBattle(Equipment eq)
    {
        DBAdapter ad = new DBAdapter();
        ad.Add("arrayType", eq.BattleArrayID);
        DBAdapter cad = new DBAdapter();
        cad.Add("equipment" + eq.ArrayIndex, eq.EquipID);
        dataManager.Update<BattleArray>(ad, cad);
    }
}
